angular.module('mamApp').controller('albumInfoController', ['$scope', '$state', '$uibModal', 'FileUploader', 'api', 'AlbumManager', 'ProgramsManager', 'ImsDialog', 'ImsAlert',
    function ($scope, $state, $uibModal, FileUploader, api, AlbumManager, ProgramsManager, ImsDialog, ImsAlert) {
        $scope.search = {
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10,
            reviewStatus: $state.params.reviewStatus || '0',
            keyword: $state.params.keyword
        };
        $scope.keyword = $scope.search.keyword;
        var albumId = $state.params.albumId;
        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        }

        $scope.videoPlay = function (program) {
            $scope.programs.forEach(function (item) {
                item.play = false;
            })
            $("#video").attr("src", program.origAudioUrl);

            program.play = !program.play;
            $("#video")[0].play();
        }
        $scope.videoStop = function (program) {
            program.play = !program.play;
            $("#video")[0].pause();
        }

        AlbumManager.albumDetail(albumId).then(function (result) {
            $scope.album = result.data;
            if(result.data.cpAlbumVO.programSort==62){
                $scope.sortableOptions = {
                    axis: 'y',
                    // called after a node is dropped
                    stop: function (e, ui) {
                        if (!programsIsChange()) {
                            return false;
                        }
                        var tmpToSave = [];
                        var num = $scope.paginationConf.totalItems+1-($scope.search.pageNo - 1) * $scope.search.pageSize;
                        $scope.programs.forEach(function (program) {
                            num--;
                            var tmp = {programId: program.programId, sort: num};
                            tmpToSave.push(tmp)
                        })

                        AlbumManager.albumProgrameSort(albumId, tmpToSave).then(function (result) {
                            if (result.data.c == "s") {
                                $state.reload();
                                ImsAlert.open({title: "排序提示", message: "修改序号成功"}).then(function (result) {

                                })
                            }
                        })
                    }
                };
            }else{
                $scope.sortableOptions = {
                    axis: 'y',
                    // called after a node is dropped
                    stop: function (e, ui) {
                        if (!programsIsChange()) {
                            return false;
                        }
                        var tmpToSave = [];
                        var num = ($scope.search.pageNo - 1) * $scope.search.pageSize;
                        $scope.programs.forEach(function (program) {
                            num++;
                            var tmp = {programId: program.programId, sort: num};
                            tmpToSave.push(tmp)
                        })

                        AlbumManager.albumProgrameSort(albumId, tmpToSave).then(function (result) {
                            if (result.data.c == "s") {
                                $state.reload();
                                ImsAlert.open({title: "排序提示", message: "修改序号成功"}).then(function (result) {

                                })
                            }
                        })
                    }
                };
            }
        })

        $scope.delete = function (program) {
            program.myObj = {
                "color" : "red",
            }
            ImsDialog.open({title: "删除确认", message: "您确定要删除该节目吗？"}).then(function (a) {
                if (a == "ok") {
                    ProgramsManager.programDelete(program.programId).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }else{
                    program.myObj = {
                        "color" : "#337ab7",
                    }
                }
            })

        }

        $scope.programs = [];



        $scope.itemClick = function (id) {
            $scope.programs.forEach(function (item) {
                if (item.programId != id) {
                    item.popOpen = false;
                }
            })
        }

        $scope.reUploadProgram = function (program) {
            var modalInstance = $uibModal.open({
                animation: true,
                size: 'normal',
                openedClass: "modal-open-normal",
                templateUrl: 'programReUpload.html',
                controller: 'programReUploadController',
                resolve: {
                    program: function () {
                        return program;
                    }
                }
            });
            modalInstance.result.then(function (result) {
                if (result == "ok") {
                    setTimeout(function () {
                        $state.reload();
                    }, 100)
                }
            })
        }


        $scope.searchClick = function (keyword) {
            $scope.search.pageNo=1;
            $scope.search.keyword = keyword;
            $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
            $state.go($state.current.name, $scope.search)

        }


        function programsIsChange() {
            var isChange = false;
            for (var i = 0;i < $scope.programs.length;i++) {
                if ($scope.programs[i].programId != $scope.programsCopy[i].programId) {
                    isChange = true;
                }
            }
            return isChange;


        }

        ProgramsManager.programsList(albumId, [$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
            $scope.programs = result.data.pageList;
            $scope.programsCopy = angular.copy($scope.programs);
            $scope.paginationConf.totalItems = result.data.total;

        })

        $scope.cancel = function (program) {
            program.popOpen = false;
        }


        $scope.sure = function (program, index) {


            var params = {targetSort: program.toSort, ownSort: program.sort};
            ProgramsManager.programSort(program.albumId, params).then(function (result) {
                if (result.data.c == "s") {
                    program.popOpen = false;
                    $state.reload();
                }
            })
        };

        $scope.keyup = function (e, keyword) {

            if (e.key == "Enter") {
                $scope.search.pageNo=1;
                $scope.search.keyword = keyword;
                $scope.search.reviewStatus = $scope.search.reviewStatus == "null" ? null : $scope.search.reviewStatus;
                $state.go($state.current.name, $scope.search)
            }
        }



    }])

